<?php

namespace App\Http\Requests;

use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class FamilyHistoryStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'record_id' => [
                'required', 'integer', 'min:1',
                Rule::exists('st_medical_records', 'id')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                    $user = \Auth::user();
                    if (!$user->isDoctorsAdmin() && !$user->isAdmin() && !$user->isPatient()) {
                        $query->where(['doctor_id' => $user->id]);
                    }
                }),
            ],
            '*.select' => 'boolean',
            '*.lineal.father' => 'boolean',
            '*.lineal.mother' => 'boolean',
            '*.lineal.brother' => 'boolean',
            '*.lineal.sister' => 'boolean',
            '*.lineal.children' => 'boolean',
            '*.notLineal.grandfather' => 'boolean',
            '*.notLineal.grandmother' => 'boolean',
            '*.notLineal.grandpa' => 'boolean',
            '*.notLineal.grandma' => 'boolean',
            '*.notLineal.uncle' => 'boolean',
            '*.notLineal.aunt' => 'boolean',
            '*.notLineal.wifeSister' => 'boolean',
            '*.notLineal.motherBrother' => 'boolean',
            '*.notLineal.other' => 'boolean',

            'extend' => 'max:6',
            'extend.*.name' => 'required_with:extend|max:20|distinct',
            'extend.*.lineal.father' => 'boolean',
            'extend.*.lineal.mother' => 'boolean',
            'extend.*.lineal.brother' => 'boolean',
            'extend.*.lineal.sister' => 'boolean',
            'extend.*.lineal.children' => 'boolean',
            'extend.*.notLineal.grandfather' => 'boolean',
            'extend.*.notLineal.grandmother' => 'boolean',
            'extend.*.notLineal.grandpa' => 'boolean',
            'extend.*.notLineal.grandma' => 'boolean',
            'extend.*.notLineal.uncle' => 'boolean',
            'extend.*.notLineal.aunt' => 'boolean',
            'extend.*.notLineal.wifeSister' => 'boolean',
            'extend.*.notLineal.motherBrother' => 'boolean',
            'extend.*.notLineal.other' => 'boolean',
            'remark' => 'nullable|string|max:255',
        ];
    }

    public function messages()
    {
        return [
            'record_id.required' => '缺少病历ID',
            'record_id.integer' => '病历ID 必须是整数。',
            'record_id.min' => '病历ID 必须大于等于 :min。',
            'record_id.exists' => '病历 不存在或无修改该病历的权限。',
            '*.select.boolean' => '输入值的类型错误。',
            '*.lineal.*.boolean' => '输入值的类型错误。',
            '*.notLineal.*.boolean' => '输入值的类型错误。',
            'extend.max' => '家族史 追加不能超过 :max组。',
            'extend.*.name.required_with' => '追加组时 组名称 不能为空。',
            'extend.*.name.max' => '追加组时 组名称 最大 :max个字符。',
            'extend.*.name.distinct' => '追加组时 组名称 不能重复。',
            'extend.*.lineal.*.boolean' => '输入值的类型错误。',
            'extend.*.notLineal.*.boolean' => '输入值的类型错误。',
            'remark.max' => '备注最大 :max 个字符',
        ];
    }
}
